package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:
        while (true) {
            System.out.println("------欢迎来到学生管理系统-----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(students);
                    break;
                case "2":
                    delStudent(students);
                    break;
                case "3":
                    changeStudent(students);
                    break;
                case "4":
                    queryStudent(students);
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的输入有误请重新输入!");
                    break;
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学号:");
            sid = sc.next();
            int index = getIndex(students, sid);
            if (index != -1) {
                System.out.println("用户已存在!");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student s1 = new Student(sid, name, age, birthday);
        students.add(s1);
        System.out.println("添加成功");

    }

    //删除学生
    public static void delStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入您要删除的学生学号:");
            String sid = sc.next();
            int index = getIndex(students, sid);
            if (index == -1) {
                System.out.println("查无此人");
            } else {
                students.remove(index);
                System.out.println("删除成功!");
                break;
            }
        }
    }

    //修改学生
    public static void changeStudent(ArrayList<Student> students) {
        Scanner sc = new Scanner(System.in);
        int index = 0;
        String sid;
        while (true) {
            System.out.println("请选择您要修改的学生学号:");
            sid = sc.next();
            index = getIndex(students, sid);
            if (index == -1) {
                System.out.println("查无此人");
            } else {
                System.out.println("请输入修改后的姓名:");
                String newName = sc.next();
                System.out.println("请输入修改后的年龄:");
                int newAge = sc.nextInt();
                System.out.println("请输入修改后的生日:");
                String newBirthday = sc.next();
                Student stu = new Student(sid, newName, newAge, newBirthday);
                students.set(index, stu);
                System.out.println("修改成功!");
                break;
            }
        }
    }

    //查看学生
    public static void queryStudent(ArrayList<Student> students) {
        if (students.size() == 0) {
            System.out.println("暂无学生信息!");
            return;
        } else {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (int i = 0; i < students.size(); i++) {
                Student s = students.get(i);
                System.out.println(s.getSid() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t\t" + s.getBirthday());
            }
        }
    }

    //排同算法
    public static int getIndex(ArrayList<Student> students, String sid) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            if (sid.equals(s.getSid())) {
                index = i;
                break;
            }
        }
        return index;
    }
}
